package cn.b.sky.mes.andon.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.b.sky.mes.andon.dao.AndonTypeDao;
import cn.b.sky.mes.andon.enums.AndonTypeStatus;
import cn.b.sky.mes.andon.model.AndonType;
import cn.b.sky.mes.andon.model.AndonTypeAttribute;
import cn.b.sky.mes.andon.model.condition.AndonTypeCondition;
import cn.b.sky.mes.andon.service.AndonTypeService;
import cn.b.sky.common.utils.DatabaseUtils;

@Service("andonTypeService")
public class AndonTypeServiceImpl implements AndonTypeService {

  private static final Logger LOG = LoggerFactory.getLogger(AndonTypeServiceImpl.class);

  @Resource
  AndonTypeDao andonTypeDao;

  @Override
  public List<AndonType> selectandonTypeListByCondition(AndonTypeCondition condition) {
    if (null == condition) {
      return null;
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return andonTypeDao.selectandonTypeListByCondition(condition);
  }

  @Override
  public int selectandonTypeListCountByCondition(AndonTypeCondition condition) {
    if (null == condition) {
      return 0;
    }
    return andonTypeDao.selectandonTypeListCountByCondition(condition);
  }

  @Override
  public List<AndonType> getAllAndonTypes(String siteCode) {

    return andonTypeDao.selectAllAndonTypes(siteCode);
  }

  @Override
  public AndonType getAndonType(String siteCode, String andonTypeCode) {
    if (StringUtils.isBlank(andonTypeCode)) {
      return null;
    }
    AndonType andonType= andonTypeDao.getAndonType(siteCode, andonTypeCode);
    if(andonType!=null){
      List<AndonTypeAttribute> andonTypeAttributes=andonTypeDao.selectAndonTypeAttribute(siteCode, andonTypeCode);
      if(andonTypeAttributes!=null){
        andonType.setAndonAttributes(andonTypeAttributes);
      }
    }
    return andonType;
  }

  @Override
  public void insertAndonType(AndonType andonType) {
    if (null != andonType) {
      andonTypeDao.insertAndonType(andonType);
      if(andonType.getAndonAttributes()!=null){
        for(AndonTypeAttribute attribute:andonType.getAndonAttributes()){
          andonTypeDao.insertAndonTypeAttribute(attribute);
        }
      }
    }
  }

  @Override
  public void updateAndonType(AndonType andonType) {
    if (null != andonType) {
      andonTypeDao.updateAndonType(andonType);
      andonTypeDao.deleteAndonTypeAttribute(andonType.getSiteCode(), andonType.getAndonTypeCode());
      if(andonType.getAndonAttributes()!=null){
        for(AndonTypeAttribute attribute:andonType.getAndonAttributes()){
          andonTypeDao.insertAndonTypeAttribute(attribute);
        }
      }
    }
  }

  @Override
  public String deleteAndonType(String siteCode, String code) {
    String message = null;
    try {
      andonTypeDao.deleteAndonTypeAttribute(siteCode, code);
      andonTypeDao.deleteAndonType(siteCode, code);
    } catch (Exception e) {
      LOG.error("{}", e);
      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
      if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
        message = "dataBaseOperationPkFail.M";
      }
    }
    return message;
  }

  @Override
  public String disableAndonType(String siteCode, String code) {
    String message = null;
    AndonType andonType = andonTypeDao.getAndonType(siteCode, code);
    if (andonType.getLockFlag() == Integer.parseInt(AndonTypeStatus.DISABLE.getValue())) {
      message = "StatusIsDisable.M";
    } else {
      andonType.setLockFlag(Integer.parseInt(AndonTypeStatus.DISABLE.getValue()));
      andonTypeDao.updateAndonTypeStatus(andonType);
    }
    return message;
  }

  @Override
  public String enableAndonType(String siteCode, String code) {
    String message = null;
    AndonType andonType = andonTypeDao.getAndonType(siteCode, code);
    if (andonType.getLockFlag() == Integer.parseInt(AndonTypeStatus.ENABLE.getValue())) {
      message = "StatusIsEnable.M";
    } else {
      andonType.setLockFlag(Integer.parseInt(AndonTypeStatus.ENABLE.getValue()));
      andonTypeDao.updateAndonTypeStatus(andonType);
    }
    return message;
  }

  @Override
  public List<AndonType> getAndonAccountListBySiteCode(String siteCode, Integer lockFlag) {
    return andonTypeDao.getAndonAccountListBySiteCode(siteCode, lockFlag);
  }

@Override
public List<AndonType> getEnableAndonTypes(String siteCode)
{
	return andonTypeDao.selectEnableAndonTypes(siteCode);
}

}
